<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>6-3 透视投影</title>
  <script src="../lib/index.js"></script>
  <style>
    * {
      margin: 0;
      padding: 0 !important;
    }
    body {
      border: 1px solid #ccc;
    }
    #canvas {
      margin: 50px auto 0;
      display: block;
      background: yellow;
    }
  </style>
</head>
<body>
  <canvas id="canvas" width="400" height="400">
    此浏览器不支持canvas
  </canvas>

  <script>
    const ctx = document.getElementById('canvas');
    const gl = ctx.getContext('webgl');

    /**
     * 创建着色器源码
     * */
    // 顶点着色器
    // gl_Position vec4(0.0, 0.0, 0.0, 1.0) x, y, z, w齐次坐标（x/w, y/w, z/w）
    const VERTEX_SHADER_SOURCE = `
      attribute vec4 aPosition;
      attribute vec4 aColor;
      varying vec4 vColor;

      uniform mat4 mat;
      void main() {
        gl_Position = mat * aPosition;
        vColor = aColor;
      }
    `;

    // 片元着色器
    // gl_FragColor vec4(1.0, 0.0, 0.0, 1.0) r, b, g, a
    const FRAGMENT_SHADER_SOURCE = `
      precision lowp float;
      varying vec4 vColor;

      void main() {
        gl_FragColor = vColor;
      }
    `;

    /**
     * 创建着色器
     * */
    const program = initShader(gl, VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE);

    const aPosition = gl.getAttribLocation(program, 'aPosition');
    const aColor = gl.getAttribLocation(program, 'aColor');
    const mat = gl.getUniformLocation(program, 'mat');

    // 创建三角形系列数据
    const points = new Float32Array([
      0.75,1.0,-0.6, 1.0,0.0,0.0,
      0.25,-1.0,-0.6, 1.0,0.0,0.0,
      1.0, -1.0,-0.6, 1.0,0.0,0.0,

      0.75,1.0,-0.5, 0.0,1.0,0.0,
      0.25,-1.0,-0.5, 0.0,1.0,0.0,
      1.0, -1.0,-0.5, 0.0,1.0,0.0,

      0.75,1.0,-0.4, 0.0,0.0,1.0,
      0.25,-1.0,-0.4, 0.0,0.0,1.0,
      1.0, -1.0,-0.4, 0.0,0.0,1.0,

      -0.75,1.0,-0.6, 1.0,0.0,0.0,
      -0.25,-1.0,-0.6, 1.0,0.0,0.0,
      -1.0, -1.0,-0.6, 1.0,0.0,0.0,

      -0.75,1.0,-0.5, 0.0,1.0,0.0,
      -0.25,-1.0,-0.5, 0.0,1.0,0.0,
      -1.0, -1.0,-0.5, 0.0,1.0,0.0,

      -0.75,1.0,-0.4, 0.0,0.0,1.0,
      -0.25,-1.0,-0.4, 0.0,0.0,1.0,
      -1.0, -1.0,-0.4, 0.0,0.0,1.0,
    ]);

    // 创建缓冲区对象
    const buffer = gl.createBuffer();

    const BYTES = points.BYTES_PER_ELEMENT;

    // webgl关联缓冲区对象
    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);

    // 顶点数据写入缓冲区对象
    gl.bufferData(gl.ARRAY_BUFFER, points, gl.STATIC_DRAW);

    // 给attribute变量赋值
    gl.vertexAttribPointer(aPosition, 3, gl.FLOAT, false, BYTES * 6, 0);

    // 激活attribute变量
    gl.enableVertexAttribArray(aPosition);

    gl.vertexAttribPointer(aColor, 3, gl.FLOAT, false, BYTES * 6, BYTES * 3);

    gl.enableVertexAttribArray(aColor);

    let eyex = 0.0;
    let eyey = -0.1;
    let eyez = 0.2;
    function draw() {
      const vm = getViewMatrix(eyex,eyey,eyez,0.0,0.0,0.0,0.0,0.6,0.0);
      const perspective = getPerspective(150, ctx.width / ctx.height, 100, 1);
      gl.enable(gl.DEPTH_TEST);
      gl.uniformMatrix4fv(mat, false, mixMatrix(vm, perspective));
      gl.drawArrays(gl.TRIANGLES, 0, 3 * 6);
    }
    draw();

    document.onkeydown = function (e) {
      switch (e.keyCode) {
        case 37: eyex += 0.01; break;         // 左
        case 38: eyey += 0.01; break;         // 上
        case 39: eyex -= 0.01; break;         // 右
        case 40: eyey -= 0.01; break;         // 下
      }
      draw();
    }
  </script>
</body>
</html>



















